/**
 * 
 */
package galacticthrone.screen;

import galacticthrone.main.GTGame;

import javax.media.opengl.GL;

import javagame.core.io.Screen.ScreenRenderer;
import javagame.core.io.video.texture.Texture;

// TODO Javadoc
/**
 * <br>
 *
 * @author Jaco van der Westhuizen
 */
public class BackgroundImage implements ScreenRenderer
{
    private final String texName;

    private float cutOff = 1.0f;
    private Texture tex = null;

    public BackgroundImage(String texName)
    {
        this.texName = texName;
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#loadAssets(javax.media.opengl.GL, boolean)
     */
    @Override
    public void loadAssets(GL gl, boolean isInitial)
    {
        if (isInitial)
            tex = GTGame.getTextureManager().grab(texName);
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#onScreenReshape(int, int)
     */
    @Override
    public void onScreenReshape(int width, int height)
    {
        if (width != 0)
            this.cutOff = 1f - ((float)height / (float)width);
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#renderForDisplay(javax.media.opengl.GL)
     */
    @Override
    public void renderForDisplay(GL gl)
    {
        if (tex == null)
            return;

        gl.glLoadIdentity();
        
        gl.glBlendFunc(GL.GL_ONE, GL.GL_ZERO);
        tex.activate(gl);

        gl.glBegin(GL.GL_QUADS);
        gl.glColor3f(1, 1, 1);
        gl.glTexCoord2f(0, cutOff);
        gl.glVertex2f(-1, -1);
        gl.glTexCoord2f(0, 1);
        gl.glVertex2f(-1,  1);
        gl.glTexCoord2f(1, 1);
        gl.glVertex2f( 1,  1);
        gl.glTexCoord2f(1, cutOff);
        gl.glVertex2f( 1, -1);
        gl.glEnd();
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#renderForPicking(javax.media.opengl.GL)
     */
    @Override
    public void renderForPicking(GL gl)
    {
    }

    /* (non-Javadoc)
     * @see javagame.core.io.Screen.ScreenRenderer#unloadAssets(javax.media.opengl.GL, boolean)
     */
    @Override
    public void unloadAssets(GL gl, boolean isFinal)
    {
        if (isFinal)
        {
            GTGame.getTextureManager().release(gl, tex);
            tex = null;
        }
    }
}
